route.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { NextRequest, NextResponse } from 'next/server';
  2. import { ResultDto } from '@/types/response/common';
  3. import { fetchJson } from '@/lib/utils/server';
  4. export async function GET(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  5. const { path } = await params;
  6. const endpoint = `/api/forum/posts/${(path ?? []).join('/')}`;
  7. const url = new URL(request.url);
  8. const res: ResultDto = await fetchJson(`${endpoint}${url.search}`, {
  9. method: 'GET'
  10. });
  11. return NextResponse.json(res);
  12. }
  13. export async function POST(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  14. const { path } = await params;
  15. const endpoint = `/api/forum/posts/${(path ?? []).join('/')}`;
  16. const res: ResultDto = await fetchJson(endpoint, {
  17. method: 'POST',
  18. body: await request.arrayBuffer(),
  19. headers: { 'Content-Type': request.headers.get('content-type') || '' }
  20. });
  21. return NextResponse.json(res);
  22. }
  23. export async function PUT(request: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  24. const { path } = await params;
  25. const endpoint = `/api/forum/posts/${(path ?? []).join('/')}`;
  26. const res: ResultDto = await fetchJson(endpoint, {
  27. method: 'PUT',
  28. body: await request.arrayBuffer(),
  29. headers: { 'Content-Type': request.headers.get('content-type') || '' }
  30. });
  31. return NextResponse.json(res);
  32. }
  33. export async function DELETE(_: NextRequest, { params }: { params: Promise<{ path?: string[] }> }) {
  34. const { path } = await params;
  35. const endpoint = `/api/forum/posts/${(path ?? []).join('/')}`;
  36. const res: ResultDto = await fetchJson(endpoint, {
  37. method: 'DELETE'
  38. });
  39. return NextResponse.json(res);
  40. }